<template>
  <div class="login-container">
    <!-- 动态背景 -->
    <div class="background">
      <div class="gradient-bg"></div>
      <div class="moving-bubbles">
        <div class="bubble" v-for="n in 6" :key="n"></div>
      </div>
    </div>

    <div class="login-box">
      <h2>TimerPOS</h2>
      <el-form ref="loginForm" :model="formData" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="formData.username" 
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="formData.password" 
            type="password" 
            placeholder="密码"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { userAPI } from '@/api/services'

const router = useRouter()
const store = useStore()
const loading = ref(false)
const loginForm = ref(null)

const formData = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!loginForm.value) return
  
  await loginForm.value.validate(async (valid) => {
    if (!valid) {
      return false
    }
    
    loading.value = true
    try {
      const result = await userAPI.login(formData.username, formData.password)
      
      if (result.access_token) {
        store.commit('SET_USER', { 
          username: formData.username, 
          token: result.access_token 
        })
        
        router.push('/')
        ElMessage.success(result.message || '登录成功')
      } else {
        ElMessage.error('登录失败：无效的响应')
      }
    } catch (error) {
      if (error.response?.data?.detail?.message) {
        ElMessage.error(error.response.data.detail.message)
      } else {
        ElMessage.error('登录失败，请稍后重试')
      }
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
/* 去掉全局滚动条 */
:global(body) {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden !important;
}

.background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.gradient-bg {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(-45deg, #1a2980, #26d0ce, #243B55, #141E30);
  background-size: 400% 400%;
  animation: gradientBG 20s ease infinite;
}

.moving-bubbles {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: -100px;
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.05));
  backdrop-filter: blur(5px);
  border-radius: 50%;
  animation: rise 20s infinite ease-in-out;
  transform: scale(0);
}

.bubble:nth-child(2n) {
  width: 20px;
  height: 20px;
  animation-duration: 15s;
  animation-delay: 1s;
}

.bubble:nth-child(3n) {
  width: 30px;
  height: 30px;
  animation-duration: 18s;
  animation-delay: 2s;
}

.bubble:nth-child(4n) {
  width: 50px;
  height: 50px;
  animation-duration: 25s;
  animation-delay: 3s;
}

@keyframes rise {
  0% {
    bottom: -100px;
    transform: scale(0) translateX(0);
    opacity: 0;
  }
  50% {
    transform: scale(1) translateX(100px);
    opacity: 0.6;
  }
  100% {
    bottom: 1080px;
    transform: scale(0.5) translateX(-200px);
    opacity: 0;
  }
}

.login-box {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  width: 400px;
  z-index: 10;
  border: 1px solid rgba(255, 255, 255, 0.1);
  animation: fadeIn 1s ease-out;
  position: relative;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

h2 {
  color: #ffffff;
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  font-weight: 400;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.login-form :deep(.el-input) {
  background: transparent;
}

.login-form :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #64b5f6;
  box-shadow: 0 0 10px rgba(100, 181, 246, 0.3);
}

.login-form :deep(.el-input__wrapper input) {
  color: #ffffff;
}

.login-form :deep(.el-input__wrapper input::placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

.login-form :deep(.el-input__prefix-inner) {
  color: rgba(255, 255, 255, 0.6);
}

.login-button {
  width: 100%;
  height: 40px;
  background: linear-gradient(45deg, #2196f3, #1976d2);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(45deg, #1976d2, #2196f3);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(33, 150, 243, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

/* 添加响应式设计 */
@media (max-width: 500px) {
  .login-box {
    width: 90%;
    padding: 20px;
  }
}

@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
</style> 